import { MachineBase } from "@/Battle/StateMechine/MachineBase.js";

import { BattleEventType } from "@/Battle/Event/BattleEvent.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { CombatEventPool } from "@/Battle/Event/CombatEventPool.js";

export class RoundMachine extends MachineBase{
    constructor(){
        super();
        this.roundCount=0;
        this.addStateHandler('start',this.onRoundStartRequest);
        this.addStateHandler('end',this.onEndRoundRequest);
    }

    currentRoundCount(){
        return this.roundCount;
    }

    onRoundStartRequest=()=>{
        this.wait();
        //检测Round结束条件
        //this.toback()
        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(
                BattleEventType.Round.Start.Request,
                this
            ),
            this.startRound
        );
    }

    startRound=()=>{
        this.wait();
        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(
                BattleEventType.Round.Start.Complete,
                this
            ),
            this.startTurn
        );
    }

    startTurn=()=>{
        ++this.roundCount;
        this.tonext();
        this.end();
    }

    onEndRoundRequest=()=>{
        this.wait();

        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(
                BattleEventType.Round.End.Request,
                this
            ),
            this.endRound
        );
    }

    endRound=()=>{
        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(
                BattleEventType.Round.End.Complete,
                this
            ),
            this.start
        );
    }
}